package com.xspace.android.xafcommon.util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import com.xspace.android.xafcommon.R;

public class PhotoHelper {
	public static Bitmap drawTextToBitmap(Context mContext, Bitmap bitmap, Bitmap bitmap2, String gText) {
		Resources resources = mContext.getResources();
		float scale = resources.getDisplayMetrics().density;
		Bitmap.Config bitmapConfig = bitmap.getConfig();
		// set default bitmap config if none
		if (bitmapConfig == null) {
			bitmapConfig = Bitmap.Config.ARGB_8888;
		}
		// resource bitmaps are imutable,
		// so we need to convert it to mutable one
		bitmap = bitmap.copy(bitmapConfig, true);
		Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), 
				bitmap.getHeight() + bitmap2.getHeight(),
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		canvas.drawBitmap(bitmap, 0, 0, null);
		canvas.drawBitmap(bitmap2, bitmap.getWidth(), 0, null);
		// new antialised Paint
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// text color - #3D3D3D
		paint.setColor(Color.RED);
		// text size in pixels
		paint.setTextSize((int) (14 * scale * 2));
		// text shadow
		paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
		// draw text to the Canvas center
		Rect bounds = new Rect();
		paint.getTextBounds(gText, 0, gText.length(), bounds);
		//      int x = (bitmap.getWidth() - bounds.width()) / 2;
		//      int y = (bitmap.getHeight() + bounds.height()) / 2;
		//draw  text  to the bottom
		int x = (bitmap.getWidth() - bounds.width()) / 10 * 9;
		int y = (bitmap.getHeight() + bitmap2.getHeight() + bounds.height()) / 10 * 9;
		canvas.drawText(gText, x, y, paint);

		return result;
	}

	// 为图片target添加水印文字  
	// Bitmap target：被添加水印的图片  
	// String mark：水印文章  
	public static Bitmap createWatermark(Context mContex, Bitmap target, String mark) {
		Resources resources = mContex.getResources();
		float scale = resources.getDisplayMetrics().density;
		Bitmap.Config bitmapConfig = target.getConfig();
		int w = target.getWidth();
		int h = target.getHeight();
		if (bitmapConfig == null) {
			bitmapConfig = Bitmap.Config.ARGB_8888;
		}
		target = target.copy(bitmapConfig, true);
		//画 下边的时间框
		Bitmap downBmp = Bitmap.createBitmap(w, h / 10, Bitmap.Config.ARGB_8888);
		Canvas downCancas = new Canvas(downBmp);
		//背景色
		downCancas.drawColor(resources.getColor(R.color.water_mark_background));
		//结果 bitmap
		Bitmap result = Bitmap.createBitmap(w, h + downBmp.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(result);
		canvas.drawBitmap(target, 0, 0, null);
		canvas.drawBitmap(downBmp, 0, h, null);
		Paint p = new Paint();
		//字体大小
		float textSize = 14 * scale * 2*getWidthScale(w);
		p.setTextSize(textSize);
		p.setTextSize(resources.getDimension(R.dimen.sms_time));
		//字体颜色
		p.setColor(resources.getColor(R.color.water_mark_textcolor));
		p.setTextAlign(Paint.Align.CENTER);
		p.setAntiAlias(true);// 去锯齿  
//		float left =  w/2-mark.length()/2*25*getWidthScale(w);
//		if(left<0){
//			left = 0;
//		}ph
//		float top =  h + downBmp.getHeight() / 2+10*getHeightScale(h);
		canvas.drawText(mark, w/2,h + downBmp.getHeight() / 2-(p.descent()+p.ascent())/2 , p);
		canvas.save(Canvas.ALL_SAVE_FLAG);
		canvas.restore();
		Log.v("PhotoHelper",
				"PhotoHelper target size: width>>>" + target.getWidth() + "height >>>" + target.getHeight() 
				+"textSize:>>>>"+textSize);
		Log.v("PhotoHelper",
				"PhotoHelper result size: width>>>" + result.getWidth() + "height >>>" + result.getHeight()
				+"textSize:>>>>"+textSize);
		return result;
	}
	private static float getWidthScale(int width){
		return (float)width/612f;
	}
	private static float getHeightScale(int height){
		return height/756;
	}
}
